package nf.action;

import nf.dao.CellPhoneDao;
import nf.dao.CellPhoneDaoImpl;
import nf.entity.CellPhone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updateServlet")
public class updateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String username =(String)session.getAttribute("username");
        String password =(String)session.getAttribute("password");

        if(username!=null||password!=null) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            String no = request.getParameter("no");
            String name = request.getParameter("name");
            Float price = Float.parseFloat(request.getParameter("price"));

            CellPhone phone = new CellPhone();
            phone.setId(id);
            phone.setNo(no);
            phone.setName(name);
            phone.setPrice(price);

            CellPhoneDao dao = new CellPhoneDaoImpl();
            boolean kk = dao.updateById(phone);
            response.sendRedirect("ServletPhone");
        }else{
            response.sendRedirect("index.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String username =(String)session.getAttribute("username");
        String password =(String)session.getAttribute("password");

        if(username!=null||password!=null) {
            String id = request.getParameter("id");
            String no = request.getParameter("no");
            String name = request.getParameter("name");
            String price = request.getParameter("price");

            request.setAttribute("id", id);
            request.setAttribute("no", no);
            request.setAttribute("name", name);
            request.setAttribute("price", price);

            request.getRequestDispatcher("update.jsp").forward(request, response);
        }else{
            response.sendRedirect("index.jsp");
        }
    }
}
